package com.ww.springboot.boot.old;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 描述：图形化界面学习
 * 1.使用线程分别控制马的速度
 *
 * @author wanwei
 * @since 2021-05-24 19:23
 */
public class StudyDemo2 extends JFrame {

    private static List<Integer> yList = new ArrayList<>(7);

    private static List<Integer> RANK_LIST = new ArrayList<>(7);

    public StudyDemo2() {
        add(new composition());
    }

    public static void main(String[] args) throws InterruptedException {
        JFrame frame = new StudyDemo2();
        frame.setTitle("赛马");
        frame.setSize(830, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        for (int i = 0; i < 8; i++) {
            yList.add(0);
            int finalI = i;
            new Thread(() -> {
                while (true) {
                    try {
                        TimeUnit.MILLISECONDS.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(yList.get(finalI)<500){
                        yList.set(finalI, yList.get(finalI) + (int) (Math.random() * 10));
                    }
                }
            }).start();
        }
    }


    static class composition extends JPanel {

        public composition() {

            Timer timer = new Timer(100, new BallMoveListener());
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(Color.BLACK);
            for (int i = 0; i < 8; i++) {
                //画赛道
                g.drawLine(100 * i, 0, 100 * i, 500);
                //给赛道编号
                g.drawString(String.valueOf(i + 1), i * 100 + 50, 250);
            }

            //画马
            g.setColor(Color.RED);
            for (int i = 0; i < 8; i++) {
                if (yList.size() <= i) {
                    yList.add(i, 0);
                }
                Integer y = yList.get(i);

                if (y > 500) {
                    if(y!=1000){
                        y = 1000;
                        RANK_LIST.add(i);
                        yList.set(i, y);
                    }
                    g.drawOval(100 * i + 50, 510, 10, 10);
                } else {
                    g.drawOval(100 * i + 50, y, 10, 10);
                }
            }

            for (int i = 0; i < RANK_LIST.size(); i++) {
                g.drawString(String.valueOf(i + 1), 100 * RANK_LIST.get(i) + 50, 540);
            }
        }

        class BallMoveListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                //重画
                repaint();
            }
        }
    }
}
